package com.appbee.gdata.picasa.server;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.util.AuthenticationException;
import com.google.inject.Provider;

public class PicasawebServiceProvider implements Provider<PicasawebService>
{
	private static final Logger log = Logger.getLogger(PicasawebServiceProvider.class.getName());
	
	private PicasawebService myService;

	private final PicasaUserAccount picasaUserAccount;
	
	@Inject
	public PicasawebServiceProvider(PicasaUserAccount picasaUserAccount)
	{
		this.picasaUserAccount = picasaUserAccount;
	}

	private void connect()
	{
		try
		{
			picasaUserAccount.ensureLoaded();
			myService = new PicasawebService("bandidmitt-1");
			myService.setUserCredentials(picasaUserAccount.getUserName(),
					picasaUserAccount.getPassword());
			picasaUserAccount.setConnected(true);
		}
		catch (AuthenticationException e)
		{
			log.log(Level.SEVERE,"Picasa AuthenticationException",e);
		}
	}

	@Override
	public PicasawebService get()
	{
		if (!picasaUserAccount.isConnected())
			connect();
		return myService;
	}

}
